package swd.basic.enviroment_argument;

import com.pub.database.*;
import javax.servlet.http.*;
import com.pub.web.*;

public class Action {
   QueryBean gqbnDAO=null;
   public String action(javax.servlet.jsp.PageContext pageContext){
      HttpServletRequest request=(HttpServletRequest)pageContext.getRequest();
      HttpServletResponse response=(HttpServletResponse)pageContext.getResponse();

      View view=(View)request.getAttribute("view");
      (new WebFunction()).setPortal_to_session(request); //set session from portal
      if (request.getSession().getAttribute("u_id")==null) {
         try{
            request.getRequestDispatcher((new StringBuffer("login.jsp")).insert(0,"/").toString()).forward(request,response);
            return "login";
         }
         catch(Exception le){
            view.setOnloadScript("can not redirect to login page...please contact IT");
            return "";
         }
      }
      //String lstrUserNO = request.getSession().getAttribute("u_id").toString();
      gqbnDAO=new QueryBean(view.getDBAlias(),view.getDBDebug(),view.getSourceCharset(),view.getTargetCharset());

      Business business=new Business(gqbnDAO);
      //Flow flow=new Flow(gqbnDAO,lstrUserNO,"SWD",view,business);

      //String lstrResult=null;
      String strResult="";

      try{
         view.setButtonVisible("0");

         if(view.getPrgAction()!=null && view.getPrgAction().equals("query") && view.getuser_no()!=null){
            strResult= business.selectTableDataToFld(view);
            if (strResult.equals("查無資料")){
               view.setAllClear();
               view.setButtonVisible("0");
               view.setOnloadScript("alert('查無資料');");
            } else {
               view.setBtnQueryShow("none");
               view.setBtnAddShow("none");
               view.setBtnSaveShow("");
               view.setuser_no_readonly(true);
            };
            view.setPrgStatus("query");
         }


         if(view.getPrgAction()!=null && view.getPrgAction().equals("add")){
            view.setAllClear();
            //flow.preAddAction(request.getSession().getAttribute("u_id").toString());
            view.setPrgStatus("add");
            view.setButtonVisible("1");
            return "";
         }
         if(view.getPrgAction()!=null && view.getPrgAction().equals("save") && view.getPrgStatus().equals("add")){
            //view.setOnloadScript("alert('" + flow.addAction() + "');");
            view.setButtonVisible("2");
            return "";
         }
         // 修改存檔
         if(view.getPrgAction()!=null && view.getPrgAction().equals("save") && view.getPrgStatus().equals("query")){
            view.setOnloadScript("alert('" + business.exeUpdSQL(view) + "');");
            view.setButtonVisible("2");
            view.setPrgStatus("save");
            return "";
         }

         // 離開
         if(view.getPrgAction()!=null && view.getPrgAction().equals("exit")){
             if (view.getPrgStatus()!=null && (view.getPrgStatus().equals("add")||view.getPrgStatus().equals("modify"))){
                 view.setAllClear();
                 view.setButtonVisible("0");
                 view.setPrgStatus("exit");
                 return "";
             } else {
                view.setAllClear();
                view.setPrgStatus("exit");
                return "";
             }
         }

         return "";
      }
      catch(Exception e){
         view.setOnloadScript("alert('Program error please contact IT.');");
         return "";
      }
      finally{
         try{
            if(gqbnDAO!=null){
               gqbnDAO.close();
            }
         }
         catch(Exception e){
            System.out.println("can not close querybean in Action.java");
         }
      }
   }
}
